package com.ck.ips.service.impl.report;

import com.ck.common.model.OperationResult;
import com.ck.common.model.ProgramDownload;
import com.ck.core.constant.BusinessConstant;
import com.ck.core.constant.MsgConstant;
import com.ck.core.util.DateUtils;
import com.ck.ips.dao.report.ReportFormsMapper;
import com.ck.ips.model.vo.*;
import com.ck.ips.model.vo.device.DeviceBasicInfoVO;
import com.ck.ips.model.vo.statics.DeviceStatusSummaryDailyVO;
import com.ck.ips.service.DeviceService;
import com.ck.ips.service.report.ReportFormsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 报表实现类
 * @author: zhanglei
 * @date: 2019/5/10 10:45
 */
@Service("reportFormsService")
public class ReportFormsServiceImpl implements ReportFormsService {

    @Autowired
    ReportFormsMapper reportFormsMapper;
    @Autowired
    private DeviceService deviceService;

    /**
     * 一整天时间的时间戳
     */
    private Long oneDayTime = 86400000L;

    /**
     * 一周天数
     */
    private static final int ONE_WEEK_DAYS = 7;

    /**
     * 获取今日零点的时间戳
     * @return
     */
    private Long getToDayTime(){
        //一天的时间戳
        long nowTime =System.currentTimeMillis();
        //今日零点的时间戳
        long todayStartTime = nowTime - (nowTime + TimeZone.getDefault().getRawOffset())% (1000*3600*24);
        return todayStartTime;
    }

    @Override
    public OperationResult<Object> getToDayDeviceInfo() {
        //获取当前总共存在多少门店
        Integer storeNum = reportFormsMapper.getStoreNumByStatus(0);
        //在线终端个数
        Integer onLineDeviceNum = reportFormsMapper.getDeviceNumByStatus(0);
        //离线终端个数
        Integer offLineDeviceNum = reportFormsMapper.getDeviceNumByStatus(1);
        //终端总数
        Integer allDeviceNum = onLineDeviceNum + offLineDeviceNum;
        //今日新增终端个数
        Long todayStartTime = getToDayTime();
        Integer toDayAddDeviceNum = reportFormsMapper.todayNewDeviceNum(todayStartTime);

        Map<String,Integer> resultMap = new HashMap<>();
        resultMap.put("storeNum",storeNum);
        resultMap.put("onLineDeviceNum",onLineDeviceNum);
        resultMap.put("offLineDeviceNum",offLineDeviceNum);
        resultMap.put("allDeviceNum",allDeviceNum);
        resultMap.put("toDayAddDeviceNum",toDayAddDeviceNum);
        return OperationResult.newSuccessOperationResult(resultMap);
    }

    @Override
    public OperationResult<Object> getToDayProgramInfo() {
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //今日自营制作节目数
        Integer makeProgramNum = reportFormsMapper.makeProgramNum(BusinessConstant.SELF_PROGRAM_TYPE,todayStartTime);
        //今日自营发布节目数
        Integer publishProgramNum = reportFormsMapper.publishProgramNum(BusinessConstant.SELF_PROGRAM_TYPE,todayStartTime);
        //今日api制作节目数
        Integer apiMakeProgramNum = reportFormsMapper.makeProgramNum(BusinessConstant.API_PROGRAM_TYPE,todayStartTime);
        //今日api发布节目数
        Integer apiPublishProgramNum = reportFormsMapper.publishProgramNum(BusinessConstant.API_PROGRAM_TYPE,todayStartTime);
        Map<String,Integer> resultMap = new HashMap<>();
        resultMap.put("makeProgramNum",makeProgramNum);
        resultMap.put("publishProgramNum",publishProgramNum);
        resultMap.put("apiMakeProgramNum",apiMakeProgramNum);
        resultMap.put("apiPublishProgramNum",apiPublishProgramNum);
        return OperationResult.newSuccessOperationResult(resultMap);
    }

    @Override
    public OperationResult<Object> getStatistics() {
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //当前时间戳
        Long nowTime = System.currentTimeMillis();

        //获取终端增播失败数
        Integer addFailedNum = reportFormsMapper.addFailedNum(todayStartTime,nowTime);
        //获取终端减播失败数
        Integer subFailedNum = reportFormsMapper.subFailedNum(todayStartTime,nowTime);
        if(subFailedNum==null){ subFailedNum = 0; }
        //终端任务发送失败数（增播，减播）
        Integer issueFailureNum = addFailedNum+subFailedNum;

        //7日未上线终端
        //从今日0点起，往前推6天的0点的时间戳（7天包含今天）
        Long sixStartTime = todayStartTime - oneDayTime*6;
        //获取7日未上线终端的数量
        Integer sevenNotLogInNum = reportFormsMapper.getOffLineNum(sixStartTime);
        Map<String,Integer> resultMap = new HashMap<>();
        resultMap.put("issueFailureNum",issueFailureNum);
        resultMap.put("sevenNotLogInNum",sevenNotLogInNum);
        return OperationResult.newSuccessOperationResult(resultMap);
    }

    @Override
    public OperationResult<Object> sevenNotLoginDeviceInfo(Integer pageNum, Integer pageSize, String startTime) {
        //7日未上线终端
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //从今日0点起，往前推6天的0点的时间戳（7天包含今天）
        Long sixStartTime = todayStartTime - oneDayTime*6;
        //传入的时间
        if(!StringUtils.isBlank(startTime)){
            try{
                sixStartTime = DateUtils.stringTimeToLongTimeStamp(startTime,DateUtils.PATTEN_YMD);
            } catch (ParseException e) {
                // doing nothing
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        Page<SevenNotLoginDeviceVO> voList = reportFormsMapper.sevenNotLoginDeviceInfo(sixStartTime);
        if(null == voList || voList.size() == 0){
            return OperationResult.newSuccessOperationResult(new PageInfo<>(new ArrayList<>()));
        }
        PageInfo<SevenNotLoginDeviceVO> pageInfo = new PageInfo<>(voList);
        return OperationResult.newSuccessOperationResult(pageInfo);
    }

    @Override
    public OperationResult<Object> sevenProgramInfo() {
        //返回数据
        List<Map<String,Object>> resultList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtils.PATTEN_YMD);
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //分别获取每天发布的节目对应的版本号
        for(int i=0; i<ONE_WEEK_DAYS; i++){
            //终止时间
            Long endTime = todayStartTime - oneDayTime*(i-1);
            //起始时间
            Long startTime = todayStartTime - oneDayTime*i;
            //获取节目列表
            List<String> programVersionList = reportFormsMapper.getProgramVersionByTime(startTime,endTime);
            //当日发布节目数
            Integer programNum = programVersionList.size();
            //当日发布素材数
            Integer fileNum = 0;
            if(programNum>0){
                fileNum = reportFormsMapper.getFileNumByProgramList(programVersionList);
            }
            //当日日期
            String time = dateFormat.format(startTime);
            Map<String,Object> oneMap = new HashMap<>();
            oneMap.put("programNum",programNum);
            oneMap.put("fileNum",fileNum);
            oneMap.put("time",time);
            resultList.add(oneMap);
        }
        return OperationResult.newSuccessOperationResult(resultList);
    }

    @Override
    public OperationResult<Object> fileAverageExposureNum() {
        //返回数据
        List<Map<String, Object>> resultList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtils.PATTEN_YYYYMMDD);
        SimpleDateFormat dateFormatRes = new SimpleDateFormat(DateUtils.PATTEN_YMD);
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //分别获取每天的素材监播数据
        for(int i=1; i<=ONE_WEEK_DAYS; i++){
            Map<String,Object> resultMap = new HashMap<>();
            //起始时间
            Long startTime = todayStartTime - oneDayTime*i;
            String time = dateFormat.format(startTime);
            List<FilePlayNumVO> filePlayNumVOList =  reportFormsMapper.getFilePlayNum(time);

            //素材总数
            Integer fileNum = filePlayNumVOList.size();
            //素材总播放次数
            Integer filePlsyNum = 0;
            for(FilePlayNumVO vo:filePlayNumVOList){
                filePlsyNum+=vo.getPlayNum();
            }
            //今日素材平均播放次数
            Integer filePlayAvgNum = 0;
            if(fileNum!=0){
                filePlayAvgNum =  filePlsyNum / fileNum;
            }
            String retTime = dateFormatRes.format(startTime);
            resultMap.put("time",retTime);
            resultMap.put("filePlayAvgNum",filePlayAvgNum);
            resultList.add(resultMap);
        }
        return OperationResult.newSuccessOperationResult(resultList);
    }

    @Override
    public OperationResult<Object> getFileDownloadNun() {
        //返回数据
        List<Map<String,Object>> resultList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtils.PATTEN_YMD);
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //分别获取每天发布成功的节目终端数据
        for(int i=1; i<=ONE_WEEK_DAYS; i++){
            Map<String,Object> resultMap = new HashMap<>();
            //终止时间
            Long endTime = todayStartTime - oneDayTime*(i-1);
            //起始时间
            Long startTime = todayStartTime - oneDayTime*i;
            //获取下发成功的单个终端节目信息
            List<ProgramDownload> list = reportFormsMapper.getInfoByTime(startTime,endTime);
            //存在一个终端一天发布了几个节目的情况
            //终端个数
            List<String> deviceCodeList = new ArrayList<>();
            //素材个数
            Integer fileNum = 0;
            for (ProgramDownload programDownload:list){
                String deviceCode = programDownload.getDeviceCode();
                String programVersion = programDownload.getProgramVersion();
                //根据节目版本号获取素材的数量
                Integer thisFileNum = reportFormsMapper.getFileNumByProgram(programVersion);
                if(!deviceCodeList.contains(deviceCode)){
                    deviceCodeList.add(deviceCode);
                }
                if(thisFileNum!=null){
                    fileNum+=thisFileNum;
                }
            }
            //获取单个终端下载的素材量
            String dateStr = dateFormat.format(startTime);
            //终端数量
            Integer deviceNum = deviceCodeList.size();
            resultMap.put("time",dateStr);
            if(deviceNum==0){
                resultMap.put("avgFileNum",0);
            }else{
                Integer avgFileNum = fileNum / deviceNum;
                resultMap.put("avgFileNum",avgFileNum);
            }
            resultList.add(resultMap);
        }
        return  OperationResult.newSuccessOperationResult(resultList);
    }

    @Override
    public OperationResult<Object> getProgramDownloadNum() {
        //返回数据
        List<Map<String,Object>> resultList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtils.PATTEN_YMD);
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //分别获取每天发布成功的节目终端数据
        for(int i=1; i<=ONE_WEEK_DAYS; i++){
            Map<String,Object> resultMap = new HashMap<>();
            //终止时间
            Long endTime = todayStartTime - oneDayTime*(i-1);
            //起始时间
            Long startTime = todayStartTime - oneDayTime*i;
            //获取下发成功的单个终端节目信息
            List<ProgramDownload> list = reportFormsMapper.getInfoByTime(startTime,endTime);
            //存在一个终端一天发布了几个节目的情况
            //终端个数
            List<String> deviceCodeList = new ArrayList<>();
            //节目个数
            Integer programNum = 0;
            for (ProgramDownload programDownload:list){
                String deviceCode = programDownload.getDeviceCode();
                String programVersion = programDownload.getProgramVersion();
                //一条数据及一个节目下发
                if(!deviceCodeList.contains(deviceCode)){
                    deviceCodeList.add(deviceCode);
                }
                programNum++;
            }
            //获取单个终端下载的素材量
            String dateStr = dateFormat.format(startTime);
            //终端数量
            Integer deviceNum = deviceCodeList.size();
            resultMap.put("time",dateStr);
            if(deviceNum==0){
                resultMap.put("downloadNum",0);
            }else{
                Integer avgProgramNum = programNum / deviceNum;
                resultMap.put("downloadNum",avgProgramNum);
            }
            resultList.add(resultMap);
        }
        return  OperationResult.newSuccessOperationResult(resultList);
    }

    @Override
    public OperationResult<Object> getDeviceByGroupId(Long deviceGroupId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<DeviceStatusVO> deviceStatusVOList = reportFormsMapper.getDeviceByGroupId(deviceGroupId);
        PageInfo<DeviceStatusVO> pageInfo = new PageInfo<>(deviceStatusVOList);
        return  OperationResult.newSuccessOperationResult(pageInfo);
    }

    @Override
    public OperationResult<Object> reportLogDay(String deviceCode, String dayTime, Integer pageNum, Integer pageSize) {
        if(StringUtils.isBlank(deviceCode)){
            return OperationResult.newSuccessTipsOperationResult(MsgConstant.DEVICE_CODE_NOT_NULL);
        }
        if(StringUtils.isBlank(dayTime)){
            return OperationResult.newSuccessTipsOperationResult(MsgConstant.SEARCH_TIME_NOT_NULL);
        }
        Long oldTime = null;
        try {
            oldTime = DateUtils.stringTimeToLongTimeStamp(dayTime,DateUtils.PATTEN_YMD);
        } catch (ParseException e) {
            return OperationResult.newValidateFailOperationResult(MsgConstant.TIME_FORMAT_ERROR);
        }
        String newDayTime =  DateUtils.longTimeStampToStringDate(oldTime,DateUtils.PATTEN_YYYYMMDD);
        PageHelper.startPage(pageNum, pageSize);
        Page<PlayLogVO> playLogVOList = reportFormsMapper.reportLogDay(deviceCode,newDayTime);
        PageInfo<PlayLogVO> pageInfo = new PageInfo<>(playLogVOList);
        return OperationResult.newSuccessOperationResult(pageInfo);
    }

    @Override
    public OperationResult<Object> reportLogMonth(String deviceCode, String monthTime, Integer pageNum, Integer pageSize) {
        if(StringUtils.isBlank(deviceCode)){
            return OperationResult.newSuccessTipsOperationResult(MsgConstant.DEVICE_CODE_NOT_NULL);
        }
        if(StringUtils.isBlank(monthTime)){
            return OperationResult.newSuccessTipsOperationResult(MsgConstant.SEARCH_TIME_NOT_NULL);
        }
        Long oldTime = null;
        try {
            oldTime = DateUtils.stringTimeToLongTimeStamp(monthTime,DateUtils.PATTEN_YM);
        } catch (ParseException e) {
            return OperationResult.newValidateFailOperationResult(MsgConstant.TIME_FORMAT_ERROR);
        }
        String newMonthTime =  DateUtils.longTimeStampToStringDate(oldTime,DateUtils.PATTEN_YYYYMM);
        PageHelper.startPage(pageNum, pageSize);
        Page<PlayLogVO> playLogVOList = reportFormsMapper.reportLogMonth(deviceCode,newMonthTime);
        PageInfo<PlayLogVO> pageInfo = new PageInfo<>(playLogVOList);
        return OperationResult.newSuccessOperationResult(pageInfo);
    }

    @Override
    public OperationResult<Object> reportLogTimeFrame(String deviceCode, String dayStartTime, String dayEndTime, Integer pageNum, Integer pageSize) {
        if(StringUtils.isBlank(deviceCode)){
            return OperationResult.newSuccessTipsOperationResult(MsgConstant.DEVICE_CODE_NOT_NULL);
        }
        if(StringUtils.isBlank(dayStartTime)){
            return OperationResult.newSuccessTipsOperationResult("请上传查询起始时间");
        }
        if(StringUtils.isBlank(dayEndTime)){
            return OperationResult.newSuccessTipsOperationResult("请上传查询终止时间");
        }
        Long oldDayStartTime = null;
        Long oldDayEndTime = null;
        try {
            oldDayStartTime = DateUtils.stringTimeToLongTimeStamp(dayStartTime,DateUtils.PATTEN_YMD);
            oldDayEndTime = DateUtils.stringTimeToLongTimeStamp(dayEndTime,DateUtils.PATTEN_YMD);
        } catch (ParseException e) {
            return OperationResult.newValidateFailOperationResult(MsgConstant.TIME_FORMAT_ERROR);
        }
        String newDayStartTime =  DateUtils.longTimeStampToStringDate(oldDayStartTime,DateUtils.PATTEN_YYYYMMDD);
        String newEndStartTime =  DateUtils.longTimeStampToStringDate(oldDayEndTime,DateUtils.PATTEN_YYYYMMDD);
        PageHelper.startPage(pageNum, pageSize);
        Page<PlayLogVO> playLogVOList = reportFormsMapper.reportLogTimeFrame(deviceCode,newDayStartTime,newEndStartTime);
        PageInfo<PlayLogVO> pageInfo = new PageInfo<>(playLogVOList);
        return OperationResult.newSuccessOperationResult(pageInfo);


    }

    @Override
    public OperationResult<Object> sevenDeviceLineInfo() {
        //返回数据（近七天数据，含当日数据）
        List<DeviceStatusSummaryDailyVO> result = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtils.PATTEN_YMD);

        // 获取当日实时数据
        DeviceStatusSummaryDailyVO todayDeviceData = new DeviceStatusSummaryDailyVO();
        todayDeviceData.setMarkDate(dateFormat.format(System.currentTimeMillis()));
        todayDeviceData.setTotalOnline(reportFormsMapper.getDeviceNumByStatus(0).longValue());
        todayDeviceData.setTotalOffline(reportFormsMapper.getDeviceNumByStatus(1).longValue());
        result.add(todayDeviceData);


        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //分别获取每天的素材监播数据
        for(int i=1; i<ONE_WEEK_DAYS; i++) {
            //起始时间
            Long startTime = todayStartTime - oneDayTime * i;
            String time = dateFormat.format(startTime);
            DeviceStatusSummaryDailyVO vo = reportFormsMapper.sevenDeviceLineInfo(time);
            if(vo==null){
                vo = new DeviceStatusSummaryDailyVO();
                vo.setMarkDate(time);
                vo.setTotalOnline(0L);
                vo.setTotalOffline(0L);
            }
            result.add(vo);
        }
        return OperationResult.newSuccessOperationResult(result);
    }

    @Override
    public OperationResult<Object> getOnLineInfo(String dayTime, String deviceCode, Integer pageNum, Integer pageSize) {
        if(StringUtils.isBlank(dayTime)){
            return OperationResult.newSuccessTipsOperationResult(MsgConstant.SEARCH_TIME_NOT_NULL);
        }
        if(StringUtils.isBlank(deviceCode)){
            return OperationResult.newSuccessTipsOperationResult(MsgConstant.DEVICE_CODE_NOT_NULL);
        }
        PageHelper.startPage(pageNum, pageSize);
        Page<DeviceOnLineVO> deviceOnLineVOS = reportFormsMapper.getOnLineInfo(dayTime,deviceCode);
        PageInfo<DeviceOnLineVO> pageInfo = new PageInfo<>(deviceOnLineVOS);
        return OperationResult.newSuccessOperationResult(pageInfo);
    }

    @Override
    public OperationResult<Object> programAverageExposureNum(String programVersion) {
        //返回数据
        List<Map<String,Object>> resultList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DateUtils.PATTEN_YYYYMMDD);
        SimpleDateFormat dateFormatRes = new SimpleDateFormat(DateUtils.PATTEN_YMD);
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //累计播放时长
        Long programDurationAll = 0L;
        //累计播放次数
        Integer programPlayNumAll = 0;
        //分别获取每天的素材监播数据
        for(int i=1; i<=ONE_WEEK_DAYS; i++){
            Map<String,Object> resultMap = new HashMap<>();
            //起始时间
            Long startTime = todayStartTime - oneDayTime*i;
            String time = dateFormat.format(startTime);

            Long programDuration = 0L;
            Integer programPlayNum = 0;
            ProgramPlayNumVO programPlayNumVO =  reportFormsMapper.getProgramPlayNum(time,programVersion);

            if(programPlayNumVO!=null){
                //今日播放总时长
                programDuration = programPlayNumVO.getPlayDuration();
                //今日播放总次数
                programPlayNum = programPlayNumVO.getPlayNum();
            }
            programDurationAll+=programDuration;
            programPlayNumAll+=programPlayNum;

            String timeRes = dateFormatRes.format(startTime);
            resultMap.put("time",timeRes);
            resultMap.put("programPlayNum",programPlayNum);
            resultList.add(resultMap);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("programDurationAll",programDurationAll);
        map.put("programPlayNumAll",programPlayNumAll);
        map.put("exposureNum",resultList);
        return OperationResult.newSuccessOperationResult(map);
    }

    @Override
    public List<FileRealTimeDataVO> getFileRealTimeData(String deviceCode, List<String> programVersionList) {
        return reportFormsMapper.getFileRealTimeData(deviceCode,programVersionList);
    }

    @Override
    public List<PlayLogVO> reportLogTimeFrameExcel(String deviceCode, String dayStartTime, String dayEndTime) {
        List<PlayLogVO> playLogVOList = reportFormsMapper.reportLogTimeFrameExcel(deviceCode,dayStartTime,dayEndTime);
        return playLogVOList;
    }

    @Override
    public XSSFWorkbook createLogExcel(List<PlayLogVO> playLogVOS,String playDate) {
        //构造excel并赋值
        //表头数据
        String[] header = {"终端组", "终端序列号", "终端mac", "统计周期", "素材", "播放总次数"};
        //声明一个工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        //生成一个表格，设置表格名称
        XSSFSheet sheet = workbook.createSheet("监播记录详情");
        //设置表格列宽度为20个字节
        sheet.setDefaultColumnWidth(20);
        //创建第一行表头
        XSSFRow headrow = sheet.createRow(0);
        //遍历添加表头
        for (int i = 0; i < header.length; i++) {
            //创建一个单元格
            XSSFCell cell = headrow.createCell(i);
            //创建一个内容对象
            XSSFRichTextString text = new XSSFRichTextString(header[i]);
            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
        }
        //向excel表中装入数据
        int i=0;
        for(PlayLogVO playLog:playLogVOS){
            i++;
            //建一行
            XSSFRow headrow1 = sheet.createRow(i);
            //建一个单元格
            XSSFCell cell = headrow1.createCell(0);
            XSSFRichTextString text = new XSSFRichTextString(playLog.getGroupName());
            cell.setCellValue(text);
            //建一个单元格
            cell = headrow1.createCell(1);
            text = new XSSFRichTextString(playLog.getDeviceSerialNum());
            cell.setCellValue(text);
            //建一个单元格
            cell = headrow1.createCell(2);
            text = new XSSFRichTextString(playLog.getDeviceMac());
            cell.setCellValue(text);
            //建一个单元格
            cell = headrow1.createCell(3);
            text = new XSSFRichTextString(playDate);
            cell.setCellValue(text);
            //建一个单元格
            cell = headrow1.createCell(4);
            text = new XSSFRichTextString(playLog.getFileName());
            cell.setCellValue(text);
            //建一个单元格
            cell = headrow1.createCell(5);
            text = new XSSFRichTextString(playLog.getPlayNum().toString());
            cell.setCellValue(text);
        }
        return workbook;
    }

    @Override
    public OperationResult<Object> addSubFailedInfo(Integer pageNum, Integer pageSize) {
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //当前时间戳
        Long nowTime = System.currentTimeMillis();
        PageHelper.startPage(pageNum, pageSize);
        Page<ProgramAddSubFailedVO> deviceOnLineVOS = reportFormsMapper.addSubFailedInfo(todayStartTime,nowTime);
        PageInfo<ProgramAddSubFailedVO> pageInfo = new PageInfo<>(deviceOnLineVOS);
        return OperationResult.newSuccessOperationResult(pageInfo);
    }

    @Override
    public OperationResult<Object> todayUntieDeviceNum() {
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        Integer untieNum = reportFormsMapper.todayUntieDeviceNum(todayStartTime);
        return OperationResult.newSuccessOperationResult(untieNum);
    }

    @Override
    public OperationResult<Object> getNewToDayProgramInfo() {
        //今日0点的时间戳
        Long todayStartTime = getToDayTime();
        //当前时间戳
        Long nowTime = System.currentTimeMillis();
        //获取今日节目列表
        List<String> programVersionList = reportFormsMapper.getProgramVersionByTime(todayStartTime,nowTime);
        //当日发布节目数
        Integer programNum = programVersionList.size();
        //当日发布素材数
        Integer fileNum = 0;
        if(programNum>0){
            fileNum = reportFormsMapper.getFileNumByProgramList(programVersionList);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("programNum",programNum);
        map.put("fileNum",fileNum);
        return OperationResult.newSuccessOperationResult(map);
    }

    @Override
    public OperationResult<Object> listTodayNewDeviceInfo(Integer pageNum, Integer pageSize) {
        List<String> deviceCodeList = deviceService.listTodayNewDeviceCode();
        if(CollectionUtils.isEmpty(deviceCodeList)){
            return OperationResult.newSuccessOperationResult(com.ck.common.page.PageInfo.init());
        }
        com.ck.common.page.PageInfo<DeviceBasicInfoVO> deviceBasicInfoVOPageInfo = deviceService.listBasicDeviceInfoByPage(pageNum, pageSize,
                null, null, null, null, null, null,
                null, null, null, null, null,
                null, null, null, deviceCodeList);
        return OperationResult.newSuccessOperationResult(null == deviceBasicInfoVOPageInfo ? com.ck.common.page.PageInfo.init() : deviceBasicInfoVOPageInfo);
    }

    @Override
    public OperationResult<Object> deviceProgramInfo() {
        // 获取所有设备编码和在线状态
        // 获取每个设备当前播放的广告数量
        Long time = System.currentTimeMillis();
        List<DeviceProgramInfoVO> infoList = reportFormsMapper.deviceProgramInfo(time);
        int ltFiveTotal = 0;
        int fiveToTenTotal = 0;
        int gtTenTotal = 0;
        int ltFiveOnLine = 0;
        int ltFiveOffLine = 0;
        int fiveToTenOnLine = 0;
        int fiveToTenOffLine = 0;
        int gtTenOnLine = 0;
        int gtTenOffLine = 0;
        for(DeviceProgramInfoVO info : infoList){
            String deviceCode = info.getDeviceCode();
            Integer deviceStatus = info.getDeviceStatus();
            Integer programCount = info.getProgramCount();
            if(programCount<5){
                ltFiveTotal ++;
                if(BusinessConstant.DEVICE_INFO_STATUS_ON_LINE==deviceStatus){
                    ltFiveOnLine ++;
                }else{
                    ltFiveOffLine ++;
                }
            }else if (programCount>10){
                gtTenTotal ++;
                if(BusinessConstant.DEVICE_INFO_STATUS_ON_LINE==deviceStatus){
                    gtTenOnLine ++;
                }else{
                    gtTenOffLine ++;
                }
            }else {
                fiveToTenTotal ++;
                if(BusinessConstant.DEVICE_INFO_STATUS_ON_LINE==deviceStatus){
                    fiveToTenOnLine ++;
                }else{
                    fiveToTenOffLine ++;
                }
            }
        }
        DeviceProgramTotalVO vo = new DeviceProgramTotalVO();
        vo.setFiveToTenOffLine(fiveToTenOffLine);
        vo.setFiveToTenOnLine(fiveToTenOnLine);
        vo.setLtFiveOffLine(ltFiveOffLine);
        vo.setLtFiveOnLine(ltFiveOnLine);
        vo.setGtTenOffLine(gtTenOffLine);
        vo.setGtTenOnLine(gtTenOnLine);
        vo.setLtFiveTotal(ltFiveTotal);
        vo.setFiveToTenTotal(fiveToTenTotal);
        vo.setGtTenTotal(gtTenTotal);
        return OperationResult.newSuccessOperationResult(vo);
    }

    /**
     * 广告数量分类查看设备详情
     * @param type 广告数量分类
     * @param pageNum 当前页
     * @param pageSize 每页条数
     * @return 设备详情
     */
    @Override
    public OperationResult<Object> deviceProgramDetail(Integer type, Integer pageNum, Integer pageSize) {
        //  根据广告数量状态分页获取详情
        Long time = System.currentTimeMillis();
        PageHelper.startPage(pageNum,pageSize);
        List<DeviceProgramInfoVO> voList = reportFormsMapper.deviceProgramDetail(type,time);
        PageInfo<DeviceProgramInfoVO> pageInfo = new PageInfo<>(voList);
        return OperationResult.newSuccessOperationResult(pageInfo);
    }

    /**
     * 导出excel
     * @param type 广告数量分类
     * @return 设备详情
     */
    @Override
    public List<DeviceProgramInfoVO> deviceProgramExcel(Integer type) {
        Long time = System.currentTimeMillis();
        return reportFormsMapper.deviceProgramDetail(type,time);
    }
}
